package qumaimai.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import qumaimai.dao.FundDao;
import qumaimai.dao.impl.FundDaoImpl;
import qumaimai.entity.Fund;
import qumaimai.entity.User;

@WebServlet("/FundServlet")
public class FundServlet  extends BaseServlet{
	FundDao dao = new FundDaoImpl();
	public void findByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		List<Fund> list = dao.findByUserId(user.getId());
		double balance = dao.getBlanaceByUserId(user.getId());
		System.out.println(list);
    	session.setAttribute("fundList", list);
    	session.setAttribute("balance", balance);
    	
		response.sendRedirect("fund.jsp");
	}
	
	public void charge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		double money = Double.parseDouble(request.getParameter("money"));
		String vipRemark = request.getParameter("vipRemark");
		List<Fund> list = dao.findByUserId(user.getId());
		double balance = list.get(0).getBalance();
		
		int n = dao.add(money, balance, vipRemark, user.getId());
		
		findByUserId(request,response);
	}
	
	public void checkStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		
		int n = dao.checkStatus(user.getId());
		if(n>0){
			response.getWriter().write("yes");
		}else{
			response.getWriter().write("no");
		}
	}
}
